/*   **********************************************************************  **
 **   Copyright notice                                                       **
 **                                                                          **
 **   (c) 2009, Around(J2)ME				                                 **
 **   All rights reserved.                                                   **
 **                                                                          **
 **   This program and the accompanying materials are made available under   **
 **   the terms of the New BSD License which accompanies this    			 **
 **   distribution.											                 **
 **   A copy is found in the textfile LICENSE.txt							 **
 **                                                                          **
 **   This copyright notice MUST APPEAR in all copies of the file!           **
 **                                                                          **
 **   Main developers:                                                       **
 **     Juri Strumpflohner		http://blog.js-development.com	             **
 **		Matthias Braunhofer		http://matthias.jimdo.com                    **
 **                                                                          **
 **  **********************************************************************  */

package com.aroundj2me.main;

import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.TextField;


public class RegisterForm extends Form implements CommandListener {
	private LoginController controller;
	
	private TextField usernameField;
	private TextField password1Field;
	private TextField password2Field;
	private TextField firstnameField;
	private TextField surnameField;
	
	//Commands
	private Command doRegister;
	private Command back;
	
	public RegisterForm(LoginController controller) {
		super("Register");
		this.controller = controller;
		initialize();
		createCommands();
		this.setCommandListener(this);
	}
	
	/**
	 * Creates the commands
	 *
	 */
	private void createCommands(){
		back = new Command("back", Command.BACK, 0);
		doRegister = new Command("register", Command.SCREEN, 0);
		this.addCommand(doRegister);
		this.addCommand(back);
	}
	
	/**
	 * initializes the UI
	 *
	 */
	private void initialize(){
		usernameField = new TextField("Username: ", "", 30, TextField.ANY);
		password1Field = new TextField("Password: ", "", 15, TextField.PASSWORD);
		password2Field = new TextField("Retype password: ", "", 15, TextField.PASSWORD);
		firstnameField = new TextField("Firstname: ", "", 30, TextField.ANY);
		surnameField = new TextField("Surname: ", "", 30, TextField.ANY);
		
		this.append(usernameField);
		this.append(password1Field);
		this.append(password2Field);
		this.append(firstnameField);
		this.append(surnameField);
	}
	
	/**
	 * Clears all the content of the fields
	 * Saves memory instead of creating new object every time
	 */
	public void resetForm(){
		for(int i=0; i<this.size(); i++){
			if(this.get(i) instanceof TextField){
				TextField tmp = (TextField)this.get(i);
				tmp.setString("");
			}
		}
	}

	/**
	 * Catches the command actions
	 */
	public void commandAction(Command c, Displayable d) {
		if(c == back){
			controller.showLoginForm();
		}else if(c == doRegister){
			controller.doRegister();
		}
	}

	/*
	 * Getters and Setters
	 */

	public TextField getFirstnameField() {
		return firstnameField;
	}

	public void setFirstnameField(TextField firstnameField) {
		this.firstnameField = firstnameField;
	}

	public TextField getPassword1Field() {
		return password1Field;
	}

	public void setPassword1Field(TextField password1Field) {
		this.password1Field = password1Field;
	}

	public TextField getPassword2Field() {
		return password2Field;
	}

	public void setPassword2Field(TextField password2Field) {
		this.password2Field = password2Field;
	}

	public TextField getSurnameField() {
		return surnameField;
	}

	public void setSurnameField(TextField surnameField) {
		this.surnameField = surnameField;
	}

	public TextField getUsernameField() {
		return usernameField;
	}

	public void setUsernameField(TextField usernameField) {
		this.usernameField = usernameField;
	}
}
